<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Validation</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="filter.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="filter.examples.sanitization.html">Sanitization »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="filter.examples.html">范例</a></li>
    <li>Validation</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="filter.examples.validation" class="section">
  <h2 class="title">Validation</h2>
  <p class="para">
   <div class="example" id="example-5059">
    <p><strong>示例 #1 Validating email addresses with <span class="function"><a href="function.filter-var.html" class="function">filter_var()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$email_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'joe@example.com'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$email_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bogus'</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$email_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Email&nbsp;address&nbsp;'</span><span style="color: #0000BB">$email_a</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid.\n"</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$email_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Email&nbsp;address&nbsp;'</span><span style="color: #0000BB">$email_b</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid.\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Email&nbsp;address&nbsp;'</span><span style="color: #0000BB">$email_b</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;invalid.\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Email address &#039;joe@example.com&#039; is considered valid.
Email address &#039;bogus&#039; is considered invalid.
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5060">
    <p><strong>示例 #2 Validating IP addresses with <span class="function"><a href="function.filter-var.html" class="function">filter_var()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ip_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ip_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'42.42'</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_IP</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"IP&nbsp;address&nbsp;'</span><span style="color: #0000BB">$ip_a</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_IP</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"IP&nbsp;address&nbsp;'</span><span style="color: #0000BB">$ip_b</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
IP address &#039;127.0.0.1&#039; is considered valid.
</pre></div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5061">
    <p><strong>示例 #3 Passing options to <span class="function"><a href="function.filter-var.html" class="function">filter_var()</a></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$int_a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$int_b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'-1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$int_c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'4'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'options'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'min_range'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'max_range'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Integer&nbsp;A&nbsp;'</span><span style="color: #0000BB">$int_a</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3).\n"</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Integer&nbsp;B&nbsp;'</span><span style="color: #0000BB">$int_b</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3).\n"</span><span style="color: #007700">;<br />}<br />if&nbsp;(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Integer&nbsp;C&nbsp;'</span><span style="color: #0000BB">$int_c</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3).\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$options</span><span style="color: #007700">[</span><span style="color: #DD0000">'options'</span><span style="color: #007700">][</span><span style="color: #DD0000">'default'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />if&nbsp;((</span><span style="color: #0000BB">$int_c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$int_c</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Integer&nbsp;C&nbsp;'</span><span style="color: #0000BB">$int_c</span><span style="color: #DD0000">'&nbsp;is&nbsp;considered&nbsp;valid&nbsp;(between&nbsp;0&nbsp;and&nbsp;3)."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Integer A &#039;1&#039; is considered valid (between 0 and 3).
Integer C &#039;1&#039; is considered valid (between 0 and 3).
</pre></div>
    </div>
   </div>
  </p>
 </div></div></div></body></html>